package org.mozhu.learning.algo.sort;

import java.util.Arrays;

public class BubbleSort implements Sort {
    @Override
    public void sort(int[] numbers) {
        if (numbers.length <= 1) return;
        for (int i = 0; i < numbers.length; i++) {
            boolean isSorted = false; // 记录某趟是否发生交换，若为false表示数组已处于有序状态
            for (int j = 0; j < numbers.length - i - 1; j++) {
                if (numbers[j] > numbers[j + 1]) {
                    int temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                    isSorted = true;
                    System.out.println(String.format("%d-%d:%s", i, j, Arrays.toString(numbers)));
                }
            }
            if (!isSorted) {
                // 若数组已处于有序状态，结束循环
                break;
            }
        }
    }
}
